
package vv.lines.view.swing;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import vv.lines.view.BallView;
import vv.lines.view.swing.utils.SwingResourceHolder;

/**
 *
 * @author vpotapenko
 */
public class SwingBallView extends SwingImageObjectView implements BallView {
    
    private Color color;
    private boolean selected;
    private float alpha = 1;
    private SwingResourceHolder resourceHolder;

    public SwingBallView(SwingResourceHolder resourceHolder) {
        this.resourceHolder = resourceHolder;
    }

    public void setColor(int color) {
        this.color = transformColor(color);
    }

    private Color transformColor(int color) {
        setImage(resourceHolder.getBallImage(color));
        switch (color) {
            case 1:
                return Color.BLUE;
            case 2:
                return Color.GRAY;
            case 3:
                return Color.GREEN;
            case 4:
                return Color.ORANGE;
            case 5:
                return Color.RED;
            case 6:
                return Color.YELLOW;
            case 7:
                return Color.MAGENTA;
            default:
                return null;
        }
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    @Override
    protected void paintObjectWithImage(Graphics g) {
        if (g instanceof Graphics2D) {
            Graphics2D g2 = (Graphics2D) g;
            Composite orig = g2.getComposite();

            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            super.paintObjectWithImage(g2);

            g2.setComposite(orig);
        } else
            super.paintObjectWithImage(g);

    }

    @Override
    protected void paintObjectWithoutImage(Graphics g) {
        if (color == null) return;
        g.setColor(color);
        g.fillOval(getX(), getY(), getWidth(), getHeight());
        if (selected) {
            g.setColor(Color.BLACK);
            g.drawOval(getX(), getY(), getWidth(), getHeight());
        }
    }

    public void setAlpha(float alpha) {
        this.alpha = alpha;

        SwingFieldComponent owner = getOwner();
        if (owner != null) owner.handleDirty(getBounds());
    }
}
